//
//  SearchTableViewController.m
//  ReviewDemo
//
//  Created by 苏沫离 on 2018/3/19.
//  Copyright © 2018年 long. All rights reserved.
//

#import "SearchTableViewController.h"
#import "Masonry.h"
#import "SearchResultViewController.h"

@interface SearchTableViewController ()
<UITableViewDelegate,UITableViewDataSource,UISearchBarDelegate,UISearchControllerDelegate,
UISearchResultsUpdating>

@property (nonatomic ,strong) NSMutableArray<NSString *> *dataArray;
@property (nonatomic ,strong) UITableView *tableView;
@property (nonatomic, strong) UISearchController *searchController;

@end

@implementation SearchTableViewController

#pragma mark - life cycle

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self setNavBar];
    self.edgesForExtendedLayout = UIRectEdgeNone;

    __weak typeof (self) weakSelf = self;
    [self.view addSubview:self.tableView];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(weakSelf.view);
    }];
    
    [self loadNetworkData];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - UITableView

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass(UITableViewCell.class) forIndexPath:indexPath];
    cell.textLabel.text = self.dataArray[indexPath.row];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

#pragma mark - UISearchResultsUpdating

- (void)updateSearchResultsForSearchController:(UISearchController *)searchController
{
    self.edgesForExtendedLayout = UIRectEdgeNone;
//
    searchController.searchResultsController.view.hidden = NO;
//
    SearchResultViewController *resultVC = (SearchResultViewController *)_searchController.searchResultsController;
    resultVC.defaultView.hidden = searchController.searchBar.text.length;
    
    
    //    resultVC.resultsArray = _tempsArray;
    [resultVC reloadTableView];
}


#pragma mark -  UISearchBarDelegate

- (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar
{
    return YES;
}

- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar
{
    UIButton *button = [_searchController.searchBar valueForKey:@"_cancelButton"];
    if (button)
    {
        [button setTitleColor:COLOR_VALUE(0x777777, 1) forState:UIControlStateNormal];
        button.titleLabel.font = [UIFont fontWithName:@"PingFang-SC-Medium" size:17];
    }
    else
    {
        
    }
}

#pragma mark - event response



#pragma mark - private methods

- (void)setNavBar
{
    self.navigationItem.title = @"搜索Demo";
}

#pragma mark - Network Request

- (void)loadNetworkData
{
    NSString *filePath = [[NSBundle mainBundle] pathForResource:@"City" ofType:@"plist"];
    NSDictionary *dict = [NSDictionary dictionaryWithContentsOfFile:filePath];
    [self.dataArray removeAllObjects];
    [self.dataArray addObjectsFromArray:dict.allKeys];
    [self.tableView reloadData];
}

#pragma mark - setter and getters

- (NSMutableArray *)dataArray
{
    if (_dataArray == nil)
    {
        _dataArray = [NSMutableArray array];
    }
    
    return  _dataArray;
}

- (UITableView *)tableView
{
    if (_tableView == nil)
    {
        UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
        tableView.dataSource = self;
        tableView.delegate = self;
        tableView.tableFooterView = UIView.new;
        
        [tableView registerClass:UITableViewCell.class forCellReuseIdentifier:NSStringFromClass(UITableViewCell.class)];

        tableView.tableHeaderView = self.searchController.searchBar;
        
        _tableView = tableView;
    }
    
    return _tableView;
}

- (UISearchController *)searchController
{
    if (_searchController == nil)
    {
        SearchResultViewController *resultVC = [[SearchResultViewController alloc] init];
        _searchController = [[UISearchController alloc] initWithSearchResultsController:resultVC];
        [_searchController.searchBar sizeToFit];
        _searchController.searchResultsUpdater = self;
        self.definesPresentationContext = YES;
        _searchController.searchBar.placeholder = @"搜索城市";
                
        UIView *view = _searchController.searchBar.subviews[0];
        for (UIView *subView in view.subviews)
        {
            if ([subView isKindOfClass:NSClassFromString(@"UISearchBarBackground")])
            {
                subView.backgroundColor = [UIColor whiteColor];
                [subView removeFromSuperview];
            }
            else if ([subView isKindOfClass:NSClassFromString(@"UISearchBarTextField")])
            {
                subView.backgroundColor = COLOR_RGBA(142, 142, 142, 0.12);
            }
        }
        

        _searchController.searchBar.delegate = self;
        
    }
    return _searchController;
}

@end
